package kr.moiba.backoffice.device.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kr.moiba.commonCd.domain.CommonCd;
import kr.moiba.commonCd.service.CommonCdSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller("backofficeDeviceCtrl")
@RequestMapping("/backoffice/device")
public class DeviceCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private CommonCdSvc commonCdSvc;
		
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> noticeList(HttpServletRequest request, @RequestParam(required=false, value="idx") Integer idx) throws Exception {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//기기
		resultMap.put("device", commonCdSvc.findbyData(idx));
		
		//기기 목록
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("hIdx", 1);
		resultMap.put("deviceList", commonCdSvc.findbyListData(condition));
		
		//제조사 목록
		condition.put("hIdx", 3);
		resultMap.put("makerList", commonCdSvc.findbyListData(condition));

		return resultMap;
	}
	
	@RequestMapping(value={"/submit.do"})
	public void submit(HttpServletRequest request, HttpServletResponse response, HttpSession session, CommonCd commonCd) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String userIp = request.getRemoteAddr();
		//기본 정보 지정
		commonCd.setCreatedId(userId);
		commonCd.setCreatedIp(userIp);
		commonCd.sethIdx(1);
		commonCd.setTitle(commonCd.getValue());
		
		if(commonCd.getIdx() != null){//update
			String fAct = ServletRequestUtils.getStringParameter(request, "fAct", "modify");
			if(fAct.equals("remove")){//삭제
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("modifiedId", userId);
				condition.put("modifiedIp", userIp);
				int[] idx = ServletRequestUtils.getIntParameters(request, "idx");
				condition.put("idx", idx);
				commonCdSvc.removeData(condition);
			}else{//수정
				commonCd.setModifiedId(userId);
				commonCd.setModifiedIp(userIp);
				commonCdSvc.modifyData(commonCd);
			}
		}else{//등록
			commonCdSvc.addData(commonCd);
		}
		response.sendRedirect("./form.do");
	}
	
	@RequestMapping("/chkDupl.do")
	public @ResponseBody String chkDuplication(HttpSession session, @RequestParam(required=false, value="idx")  String idx, @RequestParam(required=true, value="model")  String model) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("chkDupl", true);
		condition.put("hIdx", 1);
		condition.put("idx", idx);
		condition.put("model", model);
		return commonCdSvc.findbyChkDupl(condition);
	}

}